package DynamicProgramming;

public class 打家劫舍2 {

    int[] nums;

    public int rob(int[] nums) {
        if (nums.length == 1) return nums[0];
        this.nums = nums;
        return Math.max(robRange(0, nums.length - 2), robRange(1, nums.length - 1));
    }

    private int robRange(int start, int end) {
        int a = 0;
        int b = 0;
        for (int i = start; i <= end; i++) {
            int temp = Math.max(b, a + nums[i]);
            a = b;
            b = temp;
        }
        return b;
    }
}
